--
-- Author: mm
-- Date: 2018-04-05 15:28:22
--
-- LionHornRoundStartPanel
-- 狮心号角回合开始Node

local ClassRef = app.mvc.createPanelClass(...)

local LionHornNpcId = tonumber( GD:getSystemPar( "lionHeartHornMapNpcId", 19352801 ) ) -- 狮心号角中号角的 npcId

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.LionRoundStartNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setNodeEventEnabled(true)

    self._isInAnimation = false -- 是否在动画中，默认false

    display.uiAddClick( self._ui.Button_start, function() -- 立即开始
        if not self._isInAnimation then -- 没进入播放状态？
            self:_playAnimation()
            self:setVisible( false )
            self:_roundStart()
        end
    end)
    self._ui.Button_start:setVisible( app:getInst( "TeamModel" ):isMyTeam() )

    self:refresh()
    self.beforeSnapshotHandler = EM:on("photo.openphotoview", function()
        if not cc:isDead(self) then
            self:hide()
        end
    end)

    self.afterSnapshotHandler = EM:on("photo.closephotoview", function()
        if not cc:isDead(self) and not self.ishided then
            self:show()
        end
    end)
end

function ClassRef:onExit()
    EM:remove(self.beforeSnapshotHandler)
    EM:remove(self.afterSnapshotHandler)
end

-- data = message S2C_StartLionHeartHorn {
--     int32 roundNum = 1;                                  //当前回合数
--     int32 roundStartStamp = 2;                           //当前回合开始时间戳
-- }
function ClassRef:refresh()
    if self._data then
        self:setVisible( true )

        local data = self._data

        self._ui.name:setString( string.format( L("lua_code_text_241"), data.roundNum or 99 ) )

        local startTimeStamp = data.roundStartStamp -- 起始时间戳
        local curTime = math.floor(app.session:getServerTime() or app:getTime())
        local restTime = math.abs( curTime - startTimeStamp ) -- 倒计时
        if 4 >= restTime then
            restTime = 4 -- 最低3s
        end
        self:startCdDown( restTime - 3 ) -- 留3s的动画时间
    else
        self.ishided = true
        self:setVisible( false )
    end
end

-- 回合开始请求
function ClassRef:_roundStart()
    self:performWithDelay( function()
        executeFunc(self._onOperate, "roundStart")
        executeFunc(self._onCloseView)
    end, 2 )
end

function ClassRef:_playAnimation()
    self._isInAnimation = true

    local hornNpcTemp = NpcManager:getNpcById( LionHornNpcId )
    if hornNpcTemp then
        hornNpcTemp:setCurAction()
        hornNpcTemp:forcePlayAction( "work" )
        AM.playSoundById(8491)
    end
end

-- 设置cd时间
function ClassRef:startCdDown( timeSecond ) -- cd倒计时
    self._cdTime = timeSecond or 0
    self:_setCDTimeStr()
    if 0 < self._cdTime then
        if self.tickNumId then
            self:stopAction(self.tickNumId)
            self.tickNumId = nil
        end
        self.tickNumId = self:schedule(function()
            self._cdTime = self._cdTime - 1
            self:_setCDTimeStr()
            if 0 >= self._cdTime then
                self:stopAction(self.tickNumId)
                self.tickNumId = nil
                self._isInAnimation = false
                if not self._isInAnimation then -- 没进入播放状态？
                    self:_playAnimation()
                    self:setVisible( false )
                end
            end
        end,1)
    end
end
function ClassRef:_setCDTimeStr()
    if 0 >= self._cdTime then
        return
    end
    self._ui.tips:setString( string.format( L("lua_code_text_328"), self._cdTime ) )
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

function ClassRef:onCloseView( cb )
    self._onCloseView = function()
        self._isInAnimation = false
        executeFunc(cb)
    end
end

return ClassRef
